package main

import "fmt"

type Books struct {
	title   string
	author  string
	subject string
	book_id int
}

func main() {
	fmt.Println("Hello World")
	/***【数组】***/
	// 初始化和访问
	var num [3]int
	num[0], num[1], num[2] = 100, 200, 300
	var a int = num[1]
	fmt.Println("num[1] = ", a)

	var arr1 = []int{1, 2, 3, 4}
	for i := 0; i < len(arr1); i++ {
		fmt.Printf("array[%v] = %v \t", i, arr1[i])
	}
	fmt.Println()
	arr2 := []string{"tom", "jerry", "龙皓晨", "叶凡", "采儿"}
	printStrAry(arr2)
	fmt.Println()
	printStrAryRange(arr2)
	fmt.Println()
	// 多维数组
	strs := [][]string{{"a", "b", "c"}, {"d", "e"}, {"f", "g", "h"}}
	for i, v := range strs {
		fmt.Printf("array[%v] = %v \t", i, v)
	}
	fmt.Println()
	row4 := []string{"i", "j", "k"}
	strs = append(strs, row4)
	for i, v := range strs {
		fmt.Printf("append array[%v] = %v \t", i, v)
	}
	fmt.Println()
	// 数组参数和返回值-值参数和指针参数

	/***【结构体struct】***/
	book := Books{book_id: 1, title: "Go语言入门", author: "不知道", subject: "asdasddd"}
	fmt.Println("书籍ID:", book.book_id)
	fmt.Println("书籍Name:", book.title)
	fmt.Println("书籍author:", book.author)
	// 结构体作为函数参数

	/***【切片slice】 vs 数组？ ***/
	// 声明一个未指定大小的数组来定义切片
	num1 := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	printInt(&num1)
	// 使用 make() 函数来创建切片
	var num2 []int = make([]int, 5)
	num2[0] = 1
	num2[3] = 1
	num2[4] = 1
	printInt(&num2)
	// len和cap函数
	fmt.Println(len(num2), cap(num2))
	// 切片截取
	sliceNum1 := num1[:2]
	printInt(&sliceNum1)
	// append和copy函数
	num1 = append(num1, 11, 12, 13)
	fmt.Println(num1)
	num3 := make([]int, len(num1), (cap(num1))*2)
	copy(num3, num2)
	printInt(&num3)

	/***【范围range】遍历array/slice/channle/map ***/

	/***【集合Map】***/
	var map1 = make(map[string]string, 10)
	map1["1"] = "a"
	map1["2"] = "b"
	map1["3"] = "c"
	map1["4"] = "d"
	printMap(map1)

	map2 := map[string]string{"a": "1", "b": "2"}
	printMap(map2)
	// 获取元素 (ok，表示元素是否存在)
	v1 := map1["1"]
	fmt.Println(v1)
	v2, ok := map1["2"]
	fmt.Println(v2, ok)
	v3, ok := map1["99"]
	fmt.Println(v3, ok)
	// 修改元素
	map1["1"] = "111"
	fmt.Println(map1["1"])
	// 删除元素
	delete(map1, "2")
	fmt.Println(map1)

}

func printStrAry(ary []string) {
	for i := 0; i < len(ary); i++ {
		fmt.Printf("array[%v] = %v \t", i, ary[i])
	}
}

func printStrAryRange(ary []string) {
	for i, v := range ary {
		fmt.Printf("array[%v] = %v \t", i, v)
	}
}

func printInt(x *[]int) {
	for i := 0; i < len(*x); i++ {
		fmt.Printf("ixd[%v] = %v \t", i, (*x)[i])
	}
	fmt.Println()
}

func printMap(x map[string]string) {
	for k, v := range x {
		fmt.Printf("%v = %v \t", k, v)
	}
	fmt.Println()
}
